// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2024.12.20
 */

#include "CFASTParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include <boost/algorithm/string.hpp>

#include "Utility/Configuration/SGConfig.h"
#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CFASTData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CFAST);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

// 以下需要修改

enum CFASTIndex : std::uint8_t
{
    CFASTIndex_EID  = 2,
    CFASTIndex_PID  = 3,  ///< property Id
    CFASTIndex_TYPE = 4,  ///< property Id
    CFASTIndex_IDA  = 5,  ///< 节点id
    CFASTIndex_IDB  = 6,  ///< 节点id
    CFASTIndex_GS   = 7,
    CFASTIndex_GA   = 8,
    CFASTIndex_GB   = 9,
    CFASTIndex_XS   = 12,
    CFASTIndex_YS   = 13,
    CFASTIndex_ZS   = 14
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCFAST (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建临时的CFAST数据对象
    SG::DataStructure::FEM::CFASTData tempCFAST;

    // TODO(bug 001) 正确填写m_dominId，当前默认赋值为1
    tempCFAST.m_domainId = 1;

    // 初始化当前词条的索引为1
    int curIDKey = 1;

    // 遍历解析后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 跳过空白词条
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        switch (curIDKey)
        {
        case CFASTIndex_EID:
            tempCFAST.m_id = convertToI64 (curStrKey);
            break;
        case CFASTIndex_PID:
            tempCFAST.m_pId = convertToI64 (curStrKey);
            break;
        case CFASTIndex_TYPE:
            memcpy (tempCFAST.m_type, boost::algorithm::trim_copy (curStrKey).c_str (), 4);
            break;
        case CFASTIndex_IDA:
            tempCFAST.m_ida = convertToI64 (curStrKey);
            break;
        case CFASTIndex_IDB:
            tempCFAST.m_idb = convertToI64 (curStrKey);
            break;
        case CFASTIndex_GS:
            tempCFAST.m_gs = convertToI64 (curStrKey);
            break;
        case CFASTIndex_GA:
            tempCFAST.m_g[0] = convertToI64 (curStrKey);

            // tempCFAST.m_gh[0] = tempCFAST.m_g[0];
            // tempCFAST.m_gh[1] = tempCFAST.m_g[0];
            // tempCFAST.m_gh[2] = tempCFAST.m_g[0];
            // tempCFAST.m_gh[3] = tempCFAST.m_g[0];
            break;
        case CFASTIndex_GB:
            tempCFAST.m_g[1] = convertToI64 (curStrKey);

            // tempCFAST.m_gh[4] = tempCFAST.m_g[1];
            // tempCFAST.m_gh[5] = tempCFAST.m_g[1];
            // tempCFAST.m_gh[6] = tempCFAST.m_g[1];
            // tempCFAST.m_gh[7] = tempCFAST.m_g[1];
            break;
        case CFASTIndex_XS:
            tempCFAST.m_xs = convertToReal (curStrKey);
            break;
        case CFASTIndex_YS:
            tempCFAST.m_ys = convertToReal (curStrKey);
            break;
        case CFASTIndex_ZS:
            tempCFAST.m_zs = convertToReal (curStrKey);
            break;
        default:
            break;
        }
        // 增加当前词条的索引
        curIDKey++;
    }

    // 投影点 编号
    if (tempCFAST.m_g[0] == 0)
    {
        tempCFAST.m_g[0] = getVirtualGridId ();
    }

    if (tempCFAST.m_g[1] == 0)
    {
        tempCFAST.m_g[1] = getVirtualGridId ();
    }

    // 商飞客户要求， CFAST不使用额外辅助点
    bool        customized{ false };
    const auto& config = SG::Utility::SGConfig::GetInstance ().GetData ();
    if (config.Has ("CFAST"))
    {
        auto FASTSetting = config.GetValue ("CFAST");
        customized       = FASTSetting.GetValue ("SFCustomized").GetBool ();
    }

    // 辅助点 编号
    if (!customized)
    {
        tempCFAST.m_gh[0] = getVirtualGridId ();
        tempCFAST.m_gh[1] = getVirtualGridId ();
        tempCFAST.m_gh[2] = getVirtualGridId ();
        tempCFAST.m_gh[3] = getVirtualGridId ();

        tempCFAST.m_gh[4] = getVirtualGridId ();
        tempCFAST.m_gh[5] = getVirtualGridId ();
        tempCFAST.m_gh[6] = getVirtualGridId ();
        tempCFAST.m_gh[7] = getVirtualGridId ();
    }

    // 将解析后的CFAST数据添加到数据库
    return dbServiceFactory->get<IElementService> ()->Add (tempCFAST);
}
